package com.minglead.crawler.config;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
public class CrawlerDatabaseConfig {
	@Value("${spring.datasource.ds1.url}")
	private String url;
	@Value("${spring.datasource.ds1.username}")
	private String user;
	@Value("${spring.datasource.ds1.password}")
	private String password;

    @Bean("crawlerDataSource")
    public DataSource getDataSource() {
        DataSourceBuilder<HikariDataSource> db = DataSourceBuilder.create().type(HikariDataSource.class);
        db.driverClassName("com.mysql.cj.jdbc.Driver");
        db.url(url);
        db.username(user);
        db.password(password);
        HikariDataSource dataSource = db.build();
        dataSource.setIdleTimeout(60000);
        dataSource.setConnectionTimeout(60000);
        dataSource.setValidationTimeout(3000);
        // dataSource.setLoginTimeout(5);
        dataSource.setMaxLifetime(60000);
        dataSource.setMaximumPoolSize(10);
        dataSource.setMinimumIdle(1);
        return dataSource;
    }
}
